package com.example.xyr.myapplication.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Created by ylcf on 2018/8/15.
 */

public class PermissionUtils {
    private static PermissionUtils permissionUtils;

    private Activity activity;

    private PermissionUtils(Activity activity) {
        this.activity = activity;
    }

    public static PermissionUtils getInstance(Activity activity) {
        if (permissionUtils == null) {
            permissionUtils = new PermissionUtils(activity);
        }
        return permissionUtils;
    }

    /**
     * 相机动态权限
     */
    public boolean caPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.CAMERA},
                        101);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    /**
     * 录音的动态权限
     */
    public boolean raPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.RECORD_AUDIO},
                        101);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
    /**
     * 定位的动态权限
     */
    public boolean laPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        101);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
    /**
     * SD卡的动态权限
     */
    public boolean sdPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        101);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
}
